<?php

namespace addons\yungift\model;

use addons\yungift\library\ModelTrait;
use think\Db;
use think\Model;

/**
 * 配置模型
 */
class Config extends Model
{
    use ModelTrait;

    // 表名,不含前缀
    protected $name = 'yungift_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    protected $resultSetType = 'collection';

    // 分组
    const GROUP_BASE    = 'base';
    const GROUP_SMS     = 'sms';
    const GROUP_EXPRESS = 'express';
    const GROUP_CARD    = 'card';
    const GROUP_MP      = 'mp';

    /**
     * @param $group
     * @return array
     */
    public static function getAllAttributes()
    {
        $configList = Config::where([])->select();

        $results = [];
        foreach ($configList as $item) {
            if (empty($results[$item['group']])) {
                $results[$item['group']] = [];
            }
            $results[$item['group']][$item['key']] = $item['value'];
        }

        return $results;
    }

    /**
     * @param $group
     * @param $attributes
     */
    public static function saveAllAttributes($attributes)
    {
        Db::transaction(function () use ($attributes) {
            foreach ($attributes as $group => $attribute) {
                foreach ($attribute as $key => $value) {
                    if (! $config = Config::get(compact('group', 'key'))) {
                        $config = new Config();
                        $config['group'] = $group;
                    }
                    $config['key'] = $key;
                    $config['value'] = trim($value);
                    $config->allowField(true)->save();
                }
            }
        });
    }

    /**
     * @param $group
     * @return array
     */
    public static function getGroupAttributes($group)
    {
        $configList = Config::where(['group' => $group])->select();

        $results = [];
        foreach ($configList as $item) {
            $results[$item['key']] = $item['value'];
        }

        return $results;
    }

    /**
     * @param $group
     * @param $attributes
     */
    public static function saveGroupAttributes($group, $attributes)
    {
        Db::transaction(function () use ($group, $attributes) {
            foreach ($attributes as $key => $value) {
                if (! $config = Config::get(compact('group', 'key'))) {
                    $config = new Config();
                    $config['group'] = $group;
                }
                $config['key'] = $key;
                $config['value'] = trim($value);
                $config->allowField(true)->save();
            }
        });
    }

    /**
     * @param $group
     * @param $field
     * @return string
     */
    public static function getGroupAttribute($group, $field, $default = '')
    {
        $config = Config::get(['group' => $group, 'key' => $field]);

        return $config ? $config['value'] : $default;
    }
}
